package com.atguigu.tingshu.user.factory.impl;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.user.factory.StrategyFactory;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class StrategyFactoryImpl implements StrategyFactory {

    @Autowired
    private Map<String, ItemTypeStrategy> strategyMap;

    @Override
    public ItemTypeStrategy getStrategy(String itemType) {
        //  判断集合中是否有对应的实现类对象
        if (!strategyMap.containsKey(itemType)) {
            throw new GuiguException(500, "该策略" + itemType + "实现类不存在");
        }
        //  返回key对应的value数据。
        return strategyMap.get(itemType);
    }
}
